import requests,json

headers = {
        "pragma": "no-cache",
        "cache-control": "no-cache",
        "cipherversion": "2",
        "x-clienttraceid": "4284316962",
        "sec-ch-ua": "\"Chromium\";v=\"116\", \"Not)A;Brand\";v=\"24\", \"Android WebView\";v=\"116\"",
        "x-accountbrand": "1",
        "x-screen": "466*466",
        "versioncode": "201026300",
        "x-brandchannel": "1",
        "x-appid": "10003",
        "x-sign": "0hFz10064511CN@07344A033B2BC066BBE03B756A1B44037C71BF9678028C64CFCB2BDD39ADBF320D3AF6B7A5356E1989899AC0BBE95874E83DA6E6EF38B36E1722B515E80A88DC65AE84AACC0A2609BA6A0B802C781BD246CB65115483EDD47E9D80F7B6F9FCD3",
        "x-packagename": "com.huawei.health",
        "sec-ch-ua-platform": "\"Android\"",
        "x-appbrand": "3",
        "sec-ch-ua-mobile": "?1",
        "user-agent": "Mozilla/5.0 (Linux; Android 13; M2012K11AC Build/TKQ1.221114.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/116.0.0.0 Mobile Safari/537.36; HuaweiHealth",
        "x-adaptscreeninfo": "{\"454*454\":\"2.1,2.2,2.3,2.4,2.5,2.6,2.7,2.8\",\"466*466\":\"2.0\"}",
        "content-type": "application/json;charset=UTF-8",
        "accept": "application/json, text/plain, */*",
        "origin": "https://h5hosting-drcn.dbankcdn.cn",
        "x-requested-with": "com.huawei.health",
        "sec-fetch-site": "cross-site",
        "sec-fetch-mode": "cors",
        "sec-fetch-dest": "empty",
        "referer": "https://h5hosting-drcn.dbankcdn.cn/",
        "accept-encoding": "gzip, deflate, br",
        "accept-language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7"
    }

def fetch_theme_list():
    url = "https://api-drcn.theme.dbankcloud.cn/servicesupport/theme/v2/getThemeList.do"
    
    body = {
        "length": 1000,
        "chargeFlag": "1",
        "ver": "2.1",
        "listType": "2",
        "appId": "1",
        "subType": "0",
        "keyWord": "",
        "resourceType": "1",
        "sign": "0hFz10064511CN@07344A033B2BC066BBE03B756A1B44037C71BF9678028C64CFCB2BDD39ADBF320D3AF6B7A5356E1989899AC0BBE95874E83DA6E6EF38B36E1722B515E80A88DC65AE84AACC0A2609BA6A0B802C781BD246CB65115483EDD47E9D80F7B6F9FCD3",
        "sourceFlag": "1",
        "terminalType": "1",
        "deviceid": "20200912",
        "phoneId": "8878f7c2c6e373de",
        "buildNumber": "Vili-B19F5.0.0.125(C00M02)",
        "emuiVersion": "2.6,2.6,2.5,2.4,2.3,2.2,2.1",
        "authType": "ST",
        "userToken": "UT:1:10003:f10dea987533519be1a88cb74a894e3a672efdf745038533",
        "deviceType": "0",
        "contentPrivType": "1,2,4",
        "isVipVersion": 1,
        "listCode": "hottest",
        "cursor": "5000",
        "versionCode": "201026300"
    }


    try:
        response = requests.post(url, headers=headers, json=body)
        response.raise_for_status()
        theme_list = response.json()
        return theme_list
    except requests.exceptions.RequestException as e:
        print("获取主题列表失败，错误信息：", e)

#获取主题详情
def fetch_theme_detail(theme_id):
    url = f"https://api-drcn.theme.dbankcloud.cn/servicesupport/theme/v2/getDetailResourceInfo.do"
    body = {
        "sign": "0c5z10064511CN@CD525967158BFB9A83A7E539E2C9012B825D3095CA81024C4B1E501BB8DE2980",
        "hitopId": theme_id,
        "appId": "1",
        "resourceType": "1",
        "isConditionFilter": "0",
        "emuiVersion": "2.1",
        "devicemodel": "Latona-B19P1.1.2.6(C001)",
        "deviceid": "20200912",
        "isVipVersion": 1,
        "contentPrivType": "1,2,4",
        "versionCode": "201026300"
    }
    try:
        response = requests.post(url, headers=headers, json=body)
        response.raise_for_status()
        theme_detail = response.json()
        return theme_detail
    except requests.exceptions.RequestException as e:
        print("获取主题详情失败，错误信息：", e)
    


#下载图片
import os

def download_img(img_url, file_name):
    try:
        # 创建img文件夹
        if not os.path.exists("img2"):
            os.makedirs("img2")
        
        file_path = os.path.join("img2", file_name)
        
        # 判断文件是否存在
        if os.path.exists(file_path):
            print(f" {file_name} 已下载，跳过下载")
            return
        
        response = requests.get(img_url)
        response.raise_for_status()
        
        with open(file_path, "wb") as file:
            file.write(response.content)
        print(f" {file_name} 下载成功，保存在 img 文件夹中")
    except requests.exceptions.RequestException as e:
        print(f" {file_name} 下载失败，错误信息：", e)


# theme_detail = fetch_theme_detail(2180496425)
# print(json.dumps(theme_detail, indent=4, ensure_ascii=False))
# exit()


theme_list = fetch_theme_list()
print(f"共获取到 {len(theme_list['list'])} 个主题")

# for theme in theme_list['list']:
#     print(json.dumps(theme['fileInfo'], indent=4, ensure_ascii=False))
#     exit()

from multiprocessing import Pool

def download_theme_image(theme):
    title = theme['title'][0]['title']
    if len(theme['previews']) > 2:
        img3url = theme['previews'][0]['previewUrl']
        download_img(img3url, title + '.jpg')
        img3url = theme['previews'][2]['previewUrl']
        download_img(img3url, title + '_aod.jpg')
        
        # print(f"{title} 没有AOD预览图片，跳过下载")
    # img1url = theme['previews'][0]['previewUrl']
    # download_img(img1url, title + '_aod.jpg')

if __name__ == "__main__":
    with Pool() as pool:
        pool.map(download_theme_image, theme_list['list'])







